package com.example.app6.te;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.app6.R;

public class ScrollableListViewGroup extends ViewGroup {

    private ListView mListView1, mListView2, mListView3;
    private int mActivePointerId = MotionEvent.INVALID_POINTER_ID;
    private float mLastY;

    public ScrollableListViewGroup(Context context) {
        this(context, null);
    }

    public ScrollableListViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollableListViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        
        // 初始化 ListView
        mListView1 = findViewById(R.id.listView1);
        mListView2 = findViewById(R.id.listView2);
        mListView3 = findViewById(R.id.listView3);
        
        // 设置 ListView 的 OnTouchListener
        mListView1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return handleListViewTouchEvent(event, mListView1);
            }
        });

        mListView2.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return handleListViewTouchEvent(event, mListView2);
            }
        });

        mListView3.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return handleListViewTouchEvent(event, mListView3);
            }
        });
    }

    private boolean handleListViewTouchEvent(MotionEvent event, ListView listView) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                mActivePointerId = event.getPointerId(0);
                mLastY = event.getY();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                int pointerIndex = event.getActionIndex();
                if (pointerIndex < event.getPointerCount()) {
                    mActivePointerId = event.getPointerId(pointerIndex);
                    mLastY = event.getY(pointerIndex);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int pointerIndexMove = event.findPointerIndex(mActivePointerId);
                if (pointerIndexMove != -1) {
                    float deltaY = event.getY(pointerIndexMove) - mLastY;
                    mLastY = event.getY(pointerIndexMove);
                    listView.scrollBy(0, (int) -deltaY);
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
                int pointerIndexUp = event.getActionIndex();
                int pointerIdUp = event.getPointerId(pointerIndexUp);
                if (pointerIdUp == mActivePointerId) {
                    // 判断是否抬起的是当前活动的手指
                    int newPointerIndex = pointerIndexUp == 0 ? 1 : 0;
                    if (newPointerIndex < event.getPointerCount()) {
                        mActivePointerId = event.getPointerId(newPointerIndex);
                        mLastY = event.getY(newPointerIndex);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mActivePointerId = MotionEvent.INVALID_POINTER_ID;
                break;
        }
        return false;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 设置 ListView 的位置
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 测量 ListView 的大小
    }
}
